﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class News : UserControl
    {
        public News()
        {
            InitializeComponent();
            this.myMenu.Logo = "1";
            this.Loaded += new RoutedEventHandler(News_Loaded);
        }

        void News_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();

            InitLeftList(1);
            this.imgIndustry.Opacity = 0.8;
        }

        private void InitLeftList(int parent)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadNewsesCompleted += new EventHandler<ReadNewsesCompletedEventArgs>(client_ReadNewsesCompleted);
            client.ReadNewsesAsync(parent);
        }

        void client_ReadNewsesCompleted(object sender, ReadNewsesCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        HyperlinkButton lnkButton = new HyperlinkButton();
                        lnkButton.Name = e.Result[i].ID.ToString();
                        lnkButton.Content = "■ " + (e.Result[i].Title.Length > 22 ? e.Result[i].Title.Substring(0, 19) + "..." : e.Result[i].Title);
                        lnkButton.Template = Application.Current.Resources["HyperlinkButtonControlTemplate1"] as ControlTemplate;
                        lnkButton.FontSize = 12;
                        lnkButton.Margin = new Thickness(0, 5, 0, 0);
                        lnkButton.Foreground = new SolidColorBrush(Colors.Black);
                        lnkButton.FontFamily = new FontFamily("Verdana,SimSun");
                        lnkButton.Click += new RoutedEventHandler(lnkButton_Click);
                        this.spNewsList.Children.Add(lnkButton);
                    }
                    #region 面包屑导航
                    if (e.Result[0].Parent == 1)
                    {
                        this.lnkClass.Content = "企业新闻";
                        this.lnkClass.Tag = "company";
                    }
                    else
                    {
                        this.lnkClass.Content = "行业新闻";
                        this.lnkClass.Tag = "industry";
                    }
                    this.tbNavNewsTitle.Text = e.Result[0].Title;
                    this.htmlHost.SourceHtml = e.Result[0].Content;
                    this.tbkNewsTitle.Text = e.Result[0].Title;
                    this.tbkNewsTime.Text = e.Result[0].DateTime;
                    #endregion

                    this.LayoutRoot.UpdateLayout();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void lnkButton_Click(object sender, RoutedEventArgs e)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadNewsCompleted += new EventHandler<ReadNewsCompletedEventArgs>(client_ReadNewsCompleted);
            client.ReadNewsAsync(Convert.ToInt32((sender as HyperlinkButton).Name));
        }

        void client_ReadNewsCompleted(object sender, ReadNewsCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                this.htmlHost.SourceHtml = "";
                if (null != e.Result)
                {
                    this.htmlHost.SourceHtml = e.Result.Content;
                    this.tbNavNewsTitle.Text = e.Result.Title;
                    this.tbkNewsTitle.Text = e.Result.Title;
                    this.tbkNewsTime.Text = e.Result.DateTime;
                }
                this.LayoutRoot.UpdateLayout();
            }
            else
            {
                throw e.Error;
            }
        }
        
        private void imgCompany_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.imgCompanIcon.Opacity = 1;
            this.imgIndustryIcon.Opacity = 0;
            this.spNewsList.Children.Clear();
            InitLeftList(1);

            this.imgIndustry.Opacity = 0.7;
            this.imgCompany.Opacity = 1;
        }

        private void imgIndustry_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.imgCompanIcon.Opacity = 0;
            this.imgIndustryIcon.Opacity = 1;
            this.spNewsList.Children.Clear();
            InitLeftList(2);

            this.imgIndustry.Opacity = 1 ;
            this.imgCompany.Opacity = 0.7;
        }

        private void lnkHome_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Home());
        }

        private void lnkNews_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new News());
        }

        private void lnkClass_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as HyperlinkButton).Tag.ToString() == "company")
            {
                this.imgCompanIcon.Opacity = 1;
                this.imgIndustryIcon.Opacity = 0;
                this.spNewsList.Children.Clear();
                InitLeftList(1);
            }
            else
            {
                this.imgCompanIcon.Opacity = 0;
                this.imgIndustryIcon.Opacity = 1;
                this.spNewsList.Children.Clear();
                InitLeftList(2);
            }
        }
    }
}
